package pe.tnova.generic.structure.project.springmvc.plugin.builder;

import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import pe.tnova.generic.structure.project.springmvc.UI.PanelConfigInitUI;
import pe.tnova.generic.structure.project.springmvc.soruce.ProjectScanCore;
import pe.tnova.generic.structure.project.springmvc.util.ConstantesTNOVA;
import pe.tnova.generic.structure.project.springmvc.util.PluginException;

public class GenerarSourceCoreToggleNatureAction implements IObjectActionDelegate {
	
	private static final Logger LOG = Logger.getLogger(GenerarSourceCoreToggleNatureAction.class.getName());

	private ISelection selection;
	private Shell shell;
	
	@SuppressWarnings("rawtypes")
	public void run(IAction action) {		
		System.out.println("GenerarSourceCoreToggleNatureAction run");
		if (selection instanceof IStructuredSelection) {
			for (Iterator it = ((IStructuredSelection) selection).iterator(); it
					.hasNext();) {
				Object element = it.next();
				IProject project = null;
				if (element instanceof IProject) {
					project = (IProject) element;		
					ProjectScanCore projectScan = ProjectScanCore.getInstance(project);
					try {
						projectScan.scanPOM();
						projectScan.scanPersisnteceXML();
						projectScan.scanDominioConfiguracion(shell);
					}catch(PluginException e){
						LOG.log(Level.OFF,"El codigo fuente incompleto", e);		
						if(!projectScan.isFaltanArchivosIniciales()){
							PanelConfigInitUI configInitUI = new PanelConfigInitUI(shell, projectScan.getArchivoGenericoBean());
							configInitUI.create();
							if(configInitUI.open() == Window.OK){
								projectScan.setArchivoGenericoBean(configInitUI.getArchivoGenericoBean());
								try {
									projectScan.generarArchivoInit();
								} catch (CoreException x) {
									MessageDialog.openError(null, ConstantesTNOVA.RECURSOS_CORE_DAO,x.getMessage());
									LOG.log(Level.OFF, "Error al generar el codigo fuente", x);
								}
								run(action);
							}
						}
					} catch (Exception e) {
						MessageDialog.openError(null, ConstantesTNOVA.RECURSOS_CORE_DAO,e.getMessage());
						LOG.log(Level.OFF, "Error al generar el codigo fuente", e);						
					}
					
				} 
			}
		}
	}

	
	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
	}

	
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
		System.out.println("setActivePart");
	}

	
//	private void toggleNature(IProject project) {
//		try {
//			IProjectDescription description = project.getDescription();
//			String[] natures = description.getNatureIds();
//			
//			for (int i = 0; i < natures.length; ++i) {
//				if (GenericStrutureNature.NATURE_ID.equals(natures[i])) {
//					// Remove the nature
//					String[] newNatures = new String[natures.length - 1];
//					System.arraycopy(natures, 0, newNatures, 0, i);
//					System.arraycopy(natures, i + 1, newNatures, i,
//							natures.length - i - 1);
//					description.setNatureIds(newNatures);
//					project.setDescription(description, null);
//					return;
//				}
//			}
//			String[] newNatures = new String[natures.length + 1];
//			System.arraycopy(natures, 0, newNatures, 0, natures.length);
//			newNatures[natures.length] = GenericStrutureNature.NATURE_ID;
//			description.setNatureIds(newNatures);
//			project.setDescription(description, null);
//		} catch (CoreException e) {
//		}
//	}

}
